# coding=utf-8
import reversion
from django.db import models
from datetime import datetime
from core.models import Company
from gym.models import Activity, Student
from core.validators import validate_only_white_spaces
from django.utils.translation import ugettext_lazy as _

MONTHLY_SITUATIONS_CHOICES = (('0', _('Waiting Payment')), ('1', _('On Analysis')), ('2', _('Paid')), ('3', _('Canceled')))
PAYMENT_CHOICES = (('0', _('Cash')), ('1', _('Debit')), ('2', _('Credit')), ('3', _('PagSeguro')))
LAUNCHING_TYPE_CHOICES = (('0', _('Bonus')), ('1', _('Discount')), ('2', _('Credit')), ('3', _('PagSeguro')))


class Payment(models.Model):
    name = models.CharField(max_length=2, choices=PAYMENT_CHOICES, verbose_name=_('Name'))

    def pay(self):
        # TODO: implementar
        pass

    class Meta:
        verbose_name = _('Payment Form')
        verbose_name_plural = _('Payment Form')

    def __unicode__(self):
        return self.name


class Monthly(models.Model):
    competence = models.DateField(verbose_name=_('Competence'), default=datetime.now)
    value = models.DecimalField(max_digits=10, decimal_places=2, verbose_name=_('Value'), )
    activities = models.ManyToManyField(Activity, verbose_name=_('Activity'))
    student = models.ForeignKey(Student, verbose_name=_('Student'))
    situation = models.CharField(max_length=2, choices=MONTHLY_SITUATIONS_CHOICES, verbose_name=_('Situation'))
    observation = models.TextField(null=True, blank=True, validators=[validate_only_white_spaces], verbose_name=_('Observation'))
    payment = models.ForeignKey(Payment, verbose_name=_('Payment Form'))

    class Meta:
        verbose_name = _('Monthly')
        verbose_name_plural = _('Monthlies')

    def __unicode__(self):
        return "%s - %s" % (self.student, self.competence)


class Launching(models.Model):
    value = models.DecimalField(max_digits=10, decimal_places=2, verbose_name=_('Value'), )
    type = models.CharField(max_length=2, choices=LAUNCHING_TYPE_CHOICES, verbose_name=_('Type'))
    monthly = models.ForeignKey(Monthly, verbose_name=_('Monthly'))

    class Meta:
        verbose_name = _('Launching')
        verbose_name_plural = _('Launchings')

    def __unicode__(self):
        return "%s - %s" % (self.type, self.value)


class FinancialInformation(models.Model):
    base_day = models.IntegerField(verbose_name=_('Base Day'))
    student = models.OneToOneField(Student, verbose_name=_('Student'))

    class Meta:
        verbose_name = _('Financial Information')
        verbose_name_plural = _('Some Financial Information')

    def __unicode__(self):
        return str(self.base_day)


class Product(models.Model):
    name = models.CharField(max_length=100, validators=[validate_only_white_spaces], verbose_name=_('Name'))
    description = models.TextField(validators=[validate_only_white_spaces], verbose_name=_('Description'))
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name=_('Price'), )
    gym = models.ForeignKey(Company, verbose_name=_('Gym'))
    active = models.BooleanField(default=True, verbose_name=_('Active'))

    class Meta:
        verbose_name = _('Product')
        verbose_name_plural = _('Products')

    def __unicode__(self):
        return self.name


class Store(models.Model):
    description = models.TextField(null=True, blank=True, validators=[validate_only_white_spaces], verbose_name=_('Description'))
    gym = models.ForeignKey(Company, verbose_name=_('Gym'))

    class Meta:
        verbose_name = _('Store')
        verbose_name_plural = _('Stores')


class Purchase(models.Model):
    date = models.DateField(default=datetime.now, verbose_name=_('Date'))
    observation = models.TextField(null=True, blank=True, validators=[validate_only_white_spaces], verbose_name=_('Observation'))
    gym = models.ForeignKey(Company, verbose_name=_('Gym'))

    class Meta:
        verbose_name = _('Purchase')
        verbose_name_plural = _('Purchases')


class Sale(models.Model):
    date = models.DateField(default=datetime.now, verbose_name=_('Date'))
    observation = models.TextField(null=True, blank=True, validators=[validate_only_white_spaces], verbose_name=_('Observation'))
    gym = models.ForeignKey(Company, verbose_name=_('Gym'))

    class Meta:
        verbose_name = _('Sale')
        verbose_name_plural = _('Sales')


class Item(models.Model):
    amount = models.IntegerField(verbose_name=_('Amount'))
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name=_('Price'), )
    product = models.ForeignKey(Product, verbose_name=_('Product'))
    store = models.ForeignKey(Store, null=True, blank=True, verbose_name=_('Store'))
    purchase = models.ForeignKey(Purchase, null=True, blank=True, verbose_name=_('Purchase'))
    sale = models.ForeignKey(Sale, null=True, blank=True, verbose_name=_('Sale'))

    class Meta:
        verbose_name = _('Item')
        verbose_name_plural = _('Items')


reversion.register(Payment)
reversion.register(Monthly)
reversion.register(Launching)
reversion.register(FinancialInformation)
reversion.register(Product)
reversion.register(Store)
reversion.register(Purchase)
reversion.register(Sale)
reversion.register(Item)
